<?php
//
// +----------------------------------------------------------------------+
// |zen-cart Open Source E-commerce                                       |
// +----------------------------------------------------------------------+
// | Copyright (c) 2003 The zen-cart developers                           |
// |                                                                      |
// | http://www.zen-cart.com/index.php                                    |
// |                                                                      |
// | Portions Copyright (c) 2003 osCommerce                               |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the GPL license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at the following url:           |
// | http://www.zen-cart.com/license/2_0.txt.                             |
// | If you did not receive a copy of the zen-cart license and are unable |
// | to obtain it through the world-wide-web, please send a note to       |
// | license@zen-cart.com so we can mail you a copy immediately.          |
// +----------------------------------------------------------------------+
// $Id: rss2.php,v 1.0.1.34 2005-03-24 15:43:23 maurycy Exp $
//

require('includes/application_top.php');


// some settings first

// 0 - no description in feed; 1 - give it in...
define('RSS_FEED_DESCRIPTION', '1');

// how many characters in description (0 for no limit)
define('RSS_FEED_DESCRIPTION_CHARACTERS', '0');

// if zero  -all products will be en feed
// else - the limit for ne products from cart settings will be used
define('RSS_FEED_ONLY_NEW_PRODUCTS', '1');

define('RSS_GENERATOR', 'Zen Cart RSS 2.0');
define('RSS_CONTENT_COPYRIGHT', 'Copyright (C) 2007 ' . STORE_OWNER);
define('RSS_TTL', '1440'); // time to live - time after reader should refresh the info in minutes
 
/*--------- END OF CONFIGURATION ------------------*/

Header("Content-Type: text/xml; charset=" . CHARSET); 
Header("Content-disposition: inline; filename=rss.xml"); 

// get the language code...
$rsslang_sql = "select code from " . TABLE_LANGUAGES . " where languages_id = " . $_SESSION['languages_id'];

$rsslang = $db->Execute($rsslang_sql);

$rss_lang = (!$rsslang->EOF) ? $rsslang->fields['code'] : DEFAULT_LANGUAGE;

echo '<?xml version="1.0" encoding="' . CHARSET . '"?>';
?>
<rss version="2.0">
  <channel>
    <title><![CDATA[<?php echo STORE_NAME; ?>]]></title> 
    <link><![CDATA[<?php echo HTTP_SERVER . DIR_WS_CATALOG; ?>]]></link> 
    <description><![CDATA[<?php echo SITE_TAGLINE; ?>]]></description> 
    <language><![CDATA[<?php echo $rss_lang; ?>]]></language>
    <lastBuildDate><![CDATA[<?php echo date('r'); ?>]]></lastBuildDate>
    <generator><![CDATA[<?php echo RSS_GENERATOR; ?>]]></generator>
    <copyright><![CDATA[<?php echo RSS_CONTENT_COPYRIGHT; ?>]]></copyright>
    <managingEditor><![CDATA[<?php echo STORE_OWNER_EMAIL_ADDRESS . " (" . STORE_OWNER . ")"; ?>]]></managingEditor>
    <ttl><![CDATA[<?php echo RSS_TTL; ?>]]></ttl>
<?php

  $display_limit = '';
  if(RSS_FEED_ONLY_NEW_PRODUCTS) // if new products only
  {
    switch (true) {
      case (SHOW_NEW_PRODUCTS_LIMIT == '0'):
        $display_limit = '';
        break;
      case (SHOW_NEW_PRODUCTS_LIMIT == '1'):
        $display_limit = " and date_format(p.products_date_added, '%Y%m') >= date_format(now(), '%Y%m')";
        break;
      case (SHOW_NEW_PRODUCTS_LIMIT == '30'):
        $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 30';
        break;
      case (SHOW_NEW_PRODUCTS_LIMIT == '60'):
        $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 60';
        break;
      case (SHOW_NEW_PRODUCTS_LIMIT == '90'):
        $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 90';
        break;
      case (SHOW_NEW_PRODUCTS_LIMIT == '120'):
        $display_limit = ' and TO_DAYS(NOW()) - TO_DAYS(p.products_date_added) <= 120';
        break;
    }
  }


    $sql_maxdate = "select max(products_date_added) as max_date_added, max(products_last_modified) as max_date_modified ";
    $sql_maxdate .= "from " . TABLE_PRODUCTS . " ";
    $sql_maxdate .= "where products_status = 1";
    
    $maxdate = $db->Execute($sql_maxdate);
    if(!$maxdate->EOF)
    {
      $max_date_added = strtotime($maxdate->fields['max_date_added']);
      $max_date_modified = strtotime($maxdate->fields['max_date_modified']);
    
      if($max_date_added < $max_date_modified)
      {
        echo "    <pubDate><![CDATA[" . date('r', $max_date_modified) . "]]></pubDate>\n";
      }
      else
      {
        echo "    <pubDate><![CDATA[" . date('r', $max_date_added) . "]]></pubDate>\n";
      }
    }
    
    $sql_products = "select p.products_id, pd.products_name, ";
    if(RSS_FEED_DESCRIPTION)
    {
      $sql_products .= "pd.products_description, ";
    }
    $sql_products .= "p.products_date_added, p.products_last_modified, p.master_categories_id ";
    $sql_products .= "from " . TABLE_PRODUCTS . " p, ";
    $sql_products .= TABLE_PRODUCTS_DESCRIPTION . " pd ";
    $sql_products .= "where p.products_id = pd.products_id ";
    $sql_products .= "and pd.language_id = " . $_SESSION['languages_id'] . " ";
    $sql_products .= "and p.products_status = 1" . $display_limit;
    $sql_products .= " order by p.products_id";
    
    $products = $db->Execute($sql_products);
    
    while(!$products->EOF)
    {
      echo "  <item>\n";
      echo "    <title><![CDATA[" . $products->fields['products_name'] . "]]></title>\n";

      $link = zen_href_link(zen_get_info_page($products->fields['products_id']), 'products_id=' . $products->fields['products_id'], 'NONSSL', false);
      echo "    <guid>" . $link . "</guid>\n";
      
      //real link needs language information...
      $link .= '&amp;language=' . $rss_lang;
      echo "    <link>" . $link . "</link>\n";
      
      if( RSS_FEED_DESCRIPTION )
      {
        $description = $products->fields['products_description'];
        if(RSS_FEED_DESCRIPTION_CHARACTERS > 0 && strlen($description) > RSS_FEED_DESCRIPTION_CHARACTERS)
        {
          $description = substr($description, 0, RSS_FEED_DESCRIPTION_CHARACTERS) . ' ...';
        }
        echo "    <description><![CDATA[" . $description . "]]></description>\n";
      }
      echo "    <pubDate><![CDATA[" . date('r', strtotime($products->fields['products_date_added'])) . "]]></pubDate>\n";
      echo "    <author><![CDATA[" . STORE_OWNER_EMAIL_ADDRESS . " (" . STORE_OWNER . ")]]></author>\n";
      
      echo "  </item>\n";
      
      $products->MoveNext();
    }
?>
  </channel>
</rss>